package models

import (
	"context"
	"errors"
	"fmt"
	. "gin-chat/entity"
	"gorm.io/gorm/clause"
	"time"
)

//用户模型
type _CUserMgr struct {
	*_BaseMgr
}

// CUserMgr open func
func CUserMgr() *_CUserMgr {
	ctx, cancel := context.WithCancel(context.Background())
	return &_CUserMgr{_BaseMgr: &_BaseMgr{DB: db.Table("c_user"), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}}
}

// GetTableName get sql table name.获取数据库名字
func (obj *_CUserMgr) GetTableName() string {
	return "c_user"
}

// Get 获取
func (obj *_CUserMgr) Get() (result CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error

	return
}

// Gets 获取批量结果
func (obj *_CUserMgr) Gets() (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error

	return
}

//////////////////////////option case ////////////////////////////////////////////

// WithID id获取
func (obj *_CUserMgr) WithID(id int) Option {
	return optionFunc(func(o *options) { o.query["id"] = id })
}

// WithUUID uuid获取 用户号，类似于qq号的唯一id
func (obj *_CUserMgr) WithUUID(uuid int) Option {
	return optionFunc(func(o *options) { o.query["uuid"] = uuid })
}

// WithNickname nickname获取 昵称
func (obj *_CUserMgr) WithNickname(nickname string) Option {
	return optionFunc(func(o *options) { o.query["nickname"] = nickname })
}

// WithPassword password获取 密码
func (obj *_CUserMgr) WithPassword(password string) Option {
	return optionFunc(func(o *options) { o.query["password"] = password })
}

// WithCreateTime create_time获取 创建时间
func (obj *_CUserMgr) WithCreateTime(createTime time.Time) Option {
	return optionFunc(func(o *options) { o.query["create_time"] = createTime })
}

// WithUpdateTime update_time获取 修改时间
func (obj *_CUserMgr) WithUpdateTime(updateTime time.Time) Option {
	return optionFunc(func(o *options) { o.query["update_time"] = updateTime })
}

// WithAvatarURL avatar_url获取 头像
func (obj *_CUserMgr) WithAvatarURL(avatarURL string) Option {
	return optionFunc(func(o *options) { o.query["avatar_url"] = avatarURL })
}

// WithEmail email获取 邮箱
func (obj *_CUserMgr) WithEmail(email string) Option {
	return optionFunc(func(o *options) { o.query["email"] = email })
}

// GetByOption 功能选项模式获取
func (obj *_CUserMgr) GetByOption(opts ...Option) (result CUser, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error

	return
}

// GetByOptions 批量功能选项模式获取
func (obj *_CUserMgr) GetByOptions(opts ...Option) (results []*CUser, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error

	return
}

//////////////////////////enume case ////////////////////////////////////////////

// GetFromID 通过id获取内容
func (obj *_CUserMgr) GetFromID(id int) (result CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` = ?", id).Find(&result).Error

	return
}

// GetBatchFromID 批量查找
func (obj *_CUserMgr) GetBatchFromID(ids []interface{}) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` IN (?)", ids).Find(&results).Error

	return
}

// GetFromUUID 通过uuid获取内容 用户号，类似于qq号的唯一id
func (obj *_CUserMgr) GetFromUUID(uuid int) (result CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`uuid` = ?", uuid).Find(&result).Error

	return
}

// GetBatchFromUUID 批量查找 用户号，类似于qq号的唯一id
func (obj *_CUserMgr) GetBatchFromUUID(uuids []int) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`uuid` IN (?)", uuids).Find(&results).Error

	return
}

// GetFromNickname 通过nickname获取内容 昵称
func (obj *_CUserMgr) GetFromNickname(nickname string) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`nickname` = ?", nickname).Find(&results).Error

	return
}

// GetBatchFromNickname 批量查找 昵称
func (obj *_CUserMgr) GetBatchFromNickname(nicknames []string) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`nickname` IN (?)", nicknames).Find(&results).Error

	return
}

// GetFromPassword 通过password获取内容 密码
func (obj *_CUserMgr) GetFromPassword(password string) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`password` = ?", password).Find(&results).Error

	return
}

// GetBatchFromPassword 批量查找 密码
func (obj *_CUserMgr) GetBatchFromPassword(passwords []string) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`password` IN (?)", passwords).Find(&results).Error

	return
}

// GetFromCreateTime 通过create_time获取内容 创建时间
func (obj *_CUserMgr) GetFromCreateTime(createTime time.Time) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`create_time` = ?", createTime).Find(&results).Error

	return
}

// GetBatchFromCreateTime 批量查找 创建时间
func (obj *_CUserMgr) GetBatchFromCreateTime(createTimes []time.Time) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`create_time` IN (?)", createTimes).Find(&results).Error

	return
}

// GetFromUpdateTime 通过update_time获取内容 修改时间
func (obj *_CUserMgr) GetFromUpdateTime(updateTime time.Time) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`update_time` = ?", updateTime).Find(&results).Error

	return
}

// GetBatchFromUpdateTime 批量查找 修改时间
func (obj *_CUserMgr) GetBatchFromUpdateTime(updateTimes []time.Time) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`update_time` IN (?)", updateTimes).Find(&results).Error

	return
}

// GetFromAvatarURL 通过avatar_url获取内容 头像
func (obj *_CUserMgr) GetFromAvatarURL(avatarURL string) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`avatar_url` = ?", avatarURL).Find(&results).Error

	return
}

// GetBatchFromAvatarURL 批量查找 头像
func (obj *_CUserMgr) GetBatchFromAvatarURL(avatarURLs []string) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`avatar_url` IN (?)", avatarURLs).Find(&results).Error

	return
}

// GetFromEmail 通过email获取内容 邮箱
func (obj *_CUserMgr) GetFromEmail(email string) (result CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`email` = ?", email).Find(&result).Error

	return
}

// GetBatchFromEmail 批量查找 邮箱
func (obj *_CUserMgr) GetBatchFromEmail(emails []string) (results []*CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`email` IN (?)", emails).Find(&results).Error

	return
}

//////////////////////////primary index case ////////////////////////////////////////////

// FetchByPrimaryKey primay or index 获取唯一内容
func (obj *_CUserMgr) FetchByPrimaryKey(id int) (result CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` = ?", id).Find(&result).Error

	return
}

// FetchUniqueByUUID primay or index 获取唯一内容
func (obj *_CUserMgr) FetchUniqueByUUID(uuid int) (result CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`uuid` = ?", uuid).Find(&result).Error

	return
}

// FetchUniqueByEmail primay or index 获取唯一内容
func (obj *_CUserMgr) FetchUniqueByEmail(email string) (result CUser, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`email` = ?", email).Find(&result).Error

	return
}

//InsertData 数据写入
func (obj *_CUserMgr) InsertData(data CUser) error {
	fmt.Println(data)
	create := obj.Clauses(clause.Insert{Modifier: "IGNORE"}).Create(&data)
	if err := create.Error; err != nil {
		return err
	}
	row := create.RowsAffected
	if row == 0 {
		return errors.New("该邮箱已注册")
	}
	fmt.Println("行", row)
	return nil
}
